<template>
  <div class="login">
    <div class="login-container">
      <div class="head">
        <img
          class="logo"
          src="https://s.weituibao.com/1582958061265/mlogo.png"
          alt=""
        />
        <div class="name">
          <div class="title">新峰商城</div>
          <div class="tips">后台管理系统</div>
        </div>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="new">登录表示您已同意 <a href="">《服务条款》</a></div>
          <el-button type="primary" @click="submitForm">立即登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-checkbox label="下次免登陆" v-model="form.mdl"></el-checkbox>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import authRouter from "@/router/authRouter";
import md5 from "js-md5";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
        mdl: true,
      },
      rules: {
        username: [{ required: true, message: "用户名不能为空" }],
        password: [{ required: true, message: "密码不能为空" }],
      },
    };
  },
  methods: {
    submitForm() {
      // 调用子组件form的validate方法
      this.$refs.form.validate(async (valid) => {
        // valid： 表单校验是否通过
        if (valid) {
          this.$request
            .post(
              "/adminUser/login",
              {
                userName: this.form.username,
                passwordMd5: md5(this.form.password),
              },
              {
                headers: {
                  "X-Requested-With": "XMLHttpRequest",
                  token: "",
                  'Content-Type':'application/json'
                },
              }
            )
            .then((data) => {
              console.log("data", data);
              localStorage.setItem('token',data.data.data)
              if (data.data.resultCode === 200) {
                // 动态添加路由
                //this.$router.addRoute(authRouter);
                // this.$store.commit("addAuthRouter");
                this.$router.push("/manage");
              }
            });

          // const data = await this.$store.dispatch("user/login", {
          //   userName:this.form.username,
          //   passwordMd5:md5(this.form.password)
          // });

          // if (data.code === 200) {
          //   this.$router.push("/manage");
          // }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
.login {
  margin: 0 auto;
  width: 680px;
  // background: chartreuse;
  .login-container {
    width: 400px;
    height: 520px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 21px 41px 0 rgb(0 0 0 / 20%);
    margin: 0 auto;
    margin-top: 200px;
    .head {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 40px 0 20px 0;
      img {
        width: 100px;
        height: 100px;
        margin-right: 20px;
      }
      .name {
        .title {
          font-size: 28px;
          color: #1baeae;
          font-weight: 700;
        }
        .tips {
          font-size: 12px;
          color: #999;
        }
      }
    }
    .el-form {
      width: 70%;
      margin: 0 auto;
    }
  }
}
</style>